﻿
Ext.data.AbstractStore.override({

    constructor: function ()
    {
        this.callParent(arguments);
        this.addEvents('activate');
        this.on('update', this.onUpdateDependences, this);
    },


    activeRecords: [],
    activeMode: 'all',

    geta: function (name)
    {
        if (!this.activeRecords) {
            if (this.data)
                return undefined;
        }
        var value = this.activeRecords[0].get(name);
        if (this.activeMode == 'all') {
            for (var i = 1, r; r = this.activeRecords[i++];) {
                if (value !== r.get(name))
                    return undefined;
            }
        }

        return value;
    },

    seta: function (name, value)
    {
        if (!this.activeRecords.length) {
            this.activeRecords.push(this.add({})[0]);
        }

        for (var i = 0, r; r = this.activeRecords[i++];) {
            r.set(name, value);
        }
    },

    setActiveRecords: function (recs)
    {
        this.activeRecords = Ext.isArray(recs) ? recs : [recs];
        this.fireEvent('activate', this);
    },

    onUpdateDependences: $logf(function Store_onUpdateDependences(store, record, operation, modifiedFieldNames)
    {
        if (operation != Ext.data.Model.EDIT) return;

        var fields = record.fields;

        for (var i = 0, mfieldName; mfieldName = modifiedFieldNames[i++];) {
            var mfield = fields.get(mfieldName);
            if (!mfield || !mfield.dependencies || !mfield.dependencies.length || mfield._nowUpdateDependencies) continue;
            $logb('modifiedField', mfield);
            mfield._nowUpdateDependencies = true;
            for(var j = 0, dname; dname = mfield.dependencies[j++]; ) {
                var dfield = fields.get(dname);
                $log('dependency', dfield);
                if (!dfield || !dfield.convert) continue;
                record.set(dname, dfield.convert(null, record));
            }
            mfield._nowUpdateDependencies = false;
            $loge();
        }
    }),
    
    __xxx: null
});
